From 5033dab9b3e38ce187f22e90a59f8e940d73bdc0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 25 Mar 2015 12:46:21 -0700 Subject: [PATCH] Fix passing --extern and -L to doc tests Previously --extern was passed for *all* upstream dependencies, causing conflicts if some had duplicate names. Now cargo only passes --extern for libraries that were built including immediate dependencies. Cargo also additionally now properly passes `-L dependency=` instead of just a plain `-L`. Closes #1449 --- src/cargo/ops/cargo_rustc/compilation.rs | 2 +- src/cargo/ops/cargo_rustc/fingerprint.rs | 13 +------- src/cargo/ops/cargo_rustc/mod.rs | 32 ++++++++++++++++++ src/cargo/ops/cargo_test.rs | 13 +++++--- tests/test_cargo_compile_git_deps.rs | 42 ++++++++++++++++++++++++ tests/test_cargo_test.rs | 2 +- 6 files changed, 85 insertions(+), 19 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/compilation.rs b/src/cargo/ops/cargo_rustc/compilation.rs index 9e23fb60f..f5d384ebf 100644 --- a/src/cargo/ops/cargo_rustc/compilation.rs +++ b/src/cargo/ops/cargo_rustc/compilation.rs @@ -14,7 +14,7 @@ pub struct Compilation { /// /// This is currently used for passing --extern flags to rustdoc tests later /// on. - pub libraries: HashMap>, + pub libraries: HashMap>, /// An array of all tests created during this compilation. pub tests: Vec<(String, PathBuf)>, diff --git a/src/cargo/ops/cargo_rustc/fingerprint.rs b/src/cargo/ops/cargo_rustc/fingerprint.rs index 5ac3c6779..2dacbb3d6 100644 --- a/src/cargo/ops/cargo_rustc/fingerprint.rs +++ b/src/cargo/ops/cargo_rustc/fingerprint.rs @@ -58,18 +58,7 @@ pub fn prepare_target<'a, 'b>(cx: &mut Context<'a, 'b>, let mut missing_outputs = false; if !profile.doc { for filename in try!(cx.target_filenames(target, profile)).iter() { - let dst = root.join(filename); - missing_outputs |= fs::metadata(&dst).is_err(); - - if profile.test { - cx.compilation.tests.push((target.name().to_string(), dst)); - } else if target.is_bin() || target.is_example() { - cx.compilation.binaries.push(dst); - } else if target.is_lib() { - let pkgid = pkg.package_id().clone(); - cx.compilation.libraries.entry(pkgid).or_insert(Vec::new()) - .push(dst); - } + missing_outputs |= fs::metadata(root.join(filename)).is_err(); } } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index c553547e7..0415a9c7c 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -132,6 +132,38 @@ pub fn compile_targets<'a, 'b>(targets: &[(&'a Target, &'a Profile)], .display().to_string(); cx.compilation.extra_env.insert("OUT_DIR".to_string(), out_dir); + for &(target, profile) in targets { + for filename in try!(cx.target_filenames(target, profile)).iter() { + let dst = cx.out_dir(pkg, Kind::Target, target).join(filename); + if profile.test { + cx.compilation.tests.push((target.name().to_string(), dst)); + } else if target.is_bin() || target.is_example() { + cx.compilation.binaries.push(dst); + } else if target.is_lib() { + let pkgid = pkg.package_id().clone(); + cx.compilation.libraries.entry(pkgid).or_insert(Vec::new()) + .push((target.crate_name(), dst)); + if !target.is_lib() { continue } + + // Include immediate lib deps as well + for dep in cx.dep_targets(pkg, target, profile).iter() { + let (pkg, target, profile) = *dep; + let pkgid = pkg.package_id(); + if !target.is_lib() { continue } + if profile.doc { continue } + if cx.compilation.libraries.contains_key(&pkgid) { continue } + + let v = try!(cx.target_filenames(target, profile)); + let v = v.into_iter().map(|f| { + (target.crate_name(), + cx.out_dir(pkg, Kind::Target, target).join(f)) + }).collect::>(); + cx.compilation.libraries.insert(pkgid.clone(), v); + } + } + } + } + if let Some(feats) = cx.resolve.features(pkg.package_id()) { cx.compilation.features.extend(feats.iter().cloned()); } diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index ff9f4dd61..e32807e8d 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -31,8 +31,11 @@ pub fn run_tests(manifest_path: &Path, let mut p = try!(compile.rustdoc_process(&compile.package)); p.arg("--test").arg(lib) .arg("--crate-name").arg(&crate_name) - .arg("-L").arg(&compile.root_output) - .arg("-L").arg(&compile.deps_output) + .arg("-L").arg(&{ + let mut arg = OsString::from("dependency="); + arg.push(&compile.deps_output); + arg + }) .cwd(compile.package.root()); if test_args.len() > 0 { @@ -43,9 +46,9 @@ pub fn run_tests(manifest_path: &Path, p.arg("--cfg").arg(&format!("feature=\"{}\"", feat)); } - for (pkg, libs) in compile.libraries.iter() { - for lib in libs.iter() { - let mut arg = OsString::from(pkg.name()); + for (_, libs) in compile.libraries.iter() { + for &(ref name, ref lib) in libs.iter() { + let mut arg = OsString::from(name); arg.push("="); arg.push(lib); p.arg("--extern").arg(&arg); diff --git a/tests/test_cargo_compile_git_deps.rs b/tests/test_cargo_compile_git_deps.rs index f1ce6ec00..4ea280cf3 100644 --- a/tests/test_cargo_compile_git_deps.rs +++ b/tests/test_cargo_compile_git_deps.rs @@ -1674,3 +1674,45 @@ test!(dont_require_submodules_are_checked_out { assert_that(git1.cargo("build").arg("-v").cwd(&dst), execs().with_status(0)); }); + +test!(doctest_same_name { + let a2 = git_repo("a2", |p| { + p.file("Cargo.toml", r#" + [project] + name = "a" + version = "0.5.0" + authors = [] + "#) + .file("src/lib.rs", "pub fn a2() {}") + }).unwrap(); + + let a1 = git_repo("a1", |p| { + p.file("Cargo.toml", &format!(r#" + [project] + name = "a" + version = "0.5.0" + authors = [] + [dependencies] + a = {{ git = '{}' }} + "#, a2.url())) + .file("src/lib.rs", "extern crate a; pub fn a1() {}") + }).unwrap(); + + let p = project("foo") + .file("Cargo.toml", &format!(r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies] + a = {{ git = '{}' }} + "#, a1.url())) + .file("src/lib.rs", r#" + #[macro_use] + extern crate a; + "#); + + assert_that(p.cargo_process("test").arg("-v"), + execs().with_status(0)); +}); diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 6cec47e39..6cf457aee 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -1416,6 +1416,6 @@ test!(dashes_to_underscores { pub fn foo() -> i32 { 1 } "#); - assert_that(p.cargo_process("test"), + assert_that(p.cargo_process("test").arg("-v"), execs().with_status(0)); }); -- 2.30.2